package org.jiuling.netty.config;

import io.netty.channel.ChannelHandler;
import org.jiuling.basics.validation.Assertions;
import org.jiuling.netty.handler.service.BusinessHandlerService;
import org.jiuling.netty.handler.service.HeartBeatHandlerService;

/**
 * WebSocket配置
 * @author 紫灵
 * @date 2025年07月07日 21时47分14秒
 */
public class WebSocketConfig {

    /** 业务处理器 */
    private BusinessHandlerService businessHandlerService;
    /** 心跳处理器 */
    private HeartBeatHandlerService heartBeatHandlerService;
    /** 最大内容长度 */
    private int maxContentLength = 65535;
    /** 链接地址 */
    private String url;
    /** 允许路径以 URL 开否 */
    private boolean checkStartsWith = true;


    public BusinessHandlerService getBusinessHandlerService() {
        return businessHandlerService;
    }

    public int getMaxContentLength() {
        return maxContentLength;
    }

    public String getUrl() {
        return url;
    }

    public static Builder builder() {
        return new Builder();
    }

    public HeartBeatHandlerService getHeartBeatHandlerService() {
        return heartBeatHandlerService;
    }

    public boolean isCheckStartsWith() {
        return checkStartsWith;
    }

    public static class Builder {

        private final WebSocketConfig webSocketConfig;

        public Builder() {
            this.webSocketConfig = new WebSocketConfig();
        }

        /**
         * 添加业务处理器
         * @param businessHandlerService 业务处理器
         * @return 当前对象
         */
        public Builder addBusinessHandlerService(BusinessHandlerService businessHandlerService) {
            this.webSocketConfig.businessHandlerService = businessHandlerService;
            return this;
        }

        /**
         * 添加最大内容长度
         * @param maxContentLength 最大内容长度
         * @return 当前对象
         */
        public Builder addMaxContentLength(int maxContentLength) {
            this.webSocketConfig.maxContentLength = maxContentLength;
            return this;
        }

        /**
         * 添加是否允许路径以 URL 开否
         * @param checkStartsWith 是否允许路径以 URL 开否
         * @return 当前对象
         */
        public Builder addCheckStartsWith(boolean checkStartsWith) {
            this.webSocketConfig.checkStartsWith = checkStartsWith;
            return this;
        }

        /**
         * 添加心跳处理器
         * @param heartBeatHandlerService 心跳处理器
         * @return 当前对象
         */
        public Builder addHeartBeatHandlerService(HeartBeatHandlerService heartBeatHandlerService) {
            this.webSocketConfig.heartBeatHandlerService = heartBeatHandlerService;
            return this;
        }

        /**
         * 添加链接地址
         * @param url 链接地址
         * @return 当前对象
         */
        public Builder addUrl(String url) {
            this.webSocketConfig.url = url;
            return this;
        }

        public WebSocketConfig build() {
            Assertions.StringAssertions.isNotBlank("链接地址不能为空！", this.webSocketConfig.url);
            Assertions.ObjectAssertions.isNotNull("业务处理器不能为空！", this.webSocketConfig.businessHandlerService);
            return this.webSocketConfig;
        }
    }

}
